﻿using System;

namespace Silver3D.Math
{
    public static class Util
    {
        public static readonly double Eps = 1e-6;
        public static readonly double RadToDeg = 180.0 / System.Math.PI;
        public static readonly double DegToRad = System.Math.PI / 180.0;
        public static readonly double Pi = System.Math.PI;
        public static readonly double TwoPi = 2.0 * System.Math.PI;
        public static readonly double HalfPi = 0.5 * System.Math.PI;

        public static bool IsEquivalent(double a, double b)
        {
            return System.Math.Abs(a - b) < Eps;
        }

        public static void Swap<T>(ref T a, ref T b)
        {
            T temp = a;
            a = b;
            b = temp;
        }

        public static void Clamp(ref double v, double min, double max)
        {
            if (v < min) v = min;
            else if (v > max) v = max;
        }

        public static int Clamp(int v, int min, int max)
        {
            if (v < min) v = min;
            else if (v > max) v = max;
            return v;
        }

        public static void Wrap(ref double v, double min, double max)
        {
            double offset = (v - min);
            double range = (max - min);
            v = offset - (int)(offset / range) * range;
        }
    }
}
